﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;
using Demo.TableStorageDataServices.Web.Models;

namespace Demo.TableStorageDataServices.Web.Services
{
    public class AzureServiceContext : TableServiceContext
    {
        private CloudStorageAccount _cloudStorageAccount;
        public AzureServiceContext(CloudStorageAccount storageAccount)
            : base(storageAccount.TableEndpoint.ToString(), storageAccount.Credentials)
        {
            this._cloudStorageAccount = storageAccount;
        }

        public OuSuisJeBlobs GetBlobContainer()
        {
            CloudBlobClient blobStorage = _cloudStorageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobStorage.GetContainerReference("picture");
            container.CreateIfNotExist();
            return new OuSuisJeBlobs(container);
        }


        public IQueryable<Enigma> Enigmas
        {
            get { return new CloudTableQuery<Enigma>(this.CreateQuery<Enigma>("enigmas")); }
        }

        public IQueryable<Response> Responses
        {
            get { return new CloudTableQuery<Response>(this.CreateQuery<Response>("responses")); }
        }

        public IQueryable<User> Users
        {
            get { return new CloudTableQuery<User>(this.CreateQuery<User>("users")); }
        }


        public void AddEnigma(Enigma enigma)
        {
            if (enigma == null) return;

            if (String.IsNullOrEmpty(enigma.PartitionKey))
                enigma.PartitionKey = String.Format("{0}_{1}", enigma.PostalCode, enigma.Country);
            if (String.IsNullOrEmpty(enigma.RowKey))
            {
                if (enigma.Id == Guid.Empty)
                    enigma.Id = Guid.NewGuid();
                enigma.RowKey = enigma.Id.ToString();
            }

            if (enigma.Image != null)
            {
                var blob = GetBlobContainer();
                blob.CreateBlobFromByte(enigma.Image, String.Format("{0}.jpg", enigma.Id));
                BlobContainerPermissions permissions = blob.BlobContainer.GetPermissions();
                permissions.PublicAccess = BlobContainerPublicAccessType.Container;
                blob.BlobContainer.SetPermissions(permissions);
                enigma.Image = null;

                enigma.UrlImage = String.Format("http://wwzeclouddemo100511.blob.core.windows.net/picture/{0}.jpg", enigma.Id);
            }

            this.AddObject("enigmas", enigma);
            this.SaveChanges();
        }


        public void AddResponse(Response response)
        {
            if (response == null) return;


            if (String.IsNullOrEmpty(response.PartitionKey))
            {
                if (response.EnigmaId == Guid.Empty)
                    return;
                response.PartitionKey = response.EnigmaId.ToString();
            }

            if (String.IsNullOrEmpty(response.RowKey))
            {
                if (response.Id == Guid.Empty)
                    response.Id = Guid.NewGuid();
                response.RowKey = response.Id.ToString();
            }

            this.AddObject("responses", response);
            this.SaveChanges();
        }

        public void AddUser(User user)
        {
            if (user == null) return;

            if (String.IsNullOrEmpty(user.PartitionKey))
                user.PartitionKey = RoleEnvironment.CurrentRoleInstance.Id;

            if (String.IsNullOrEmpty(user.RowKey))
            {
                if (user.Id == Guid.Empty)
                    user.Id = Guid.NewGuid();
                user.RowKey = user.Id.ToString();
            }

            this.AddObject("users", user);
            this.SaveChanges();
        }
    }
}